package org.idea.spring.framework.event;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * @Author linhao
 * @Date created in 4:30 下午 2021/5/23
 */
@EnableAsync
public class AnnotationEventDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(AnnotationEventDemo.class);
        annotationConfigApplicationContext.addApplicationListener(new ApplicationListener<ApplicationEvent>() {
            //这一块会优先执行
            @Override
            public void onApplicationEvent(ApplicationEvent event) {
//                System.out.println("=== application listener===");
            }
        });
        annotationConfigApplicationContext.refresh();
        annotationConfigApplicationContext.start();
        annotationConfigApplicationContext.close();
    }

    //多事件处理的时候需要对参数做区别 监听数据处理的时候，需要注意先后顺序，因为反射中的getMethod是无顺序的
    //@order的生效规则需要事件参数都是一致的时候才能生效

    @EventListener
    @Async
//    @Order(1)
    public void rejectNewJob(ContextClosedEvent contextClosedEvent) throws InterruptedException {
        System.out.println("EventListener -- 1 接收事件参数：" + contextClosedEvent);
        Thread.sleep(1000);
    }

    @EventListener
//    @Order(2)
    public void stopRunningJob(ContextClosedEvent contextClosedEvent) throws InterruptedException {
        System.out.println("EventListener -- 2 接收事件参数：" + contextClosedEvent);
    }

    @EventListener
//    @Order(3)
    public void closeRedis(ContextClosedEvent contextClosedEvent) throws InterruptedException {
        System.out.println("EventListener -- 3 接收事件参数：" + contextClosedEvent);
    }

    @EventListener
//    @Order(4)
    public void closeDatasource(ContextClosedEvent contextClosedEvent) throws InterruptedException {
        System.out.println("EventListener -- 4 接收事件参数：" + contextClosedEvent);
    }

    @EventListener
//    @Order(5)
    public void stopContainer(ContextClosedEvent contextClosedEvent) throws InterruptedException {
        System.out.println("EventListener -- 5 接收事件参数：" + contextClosedEvent);
    }


//
//    @EventListener
//    @Async
//////    @Order(3)
//    public void onApplicationEvent(ContextStartedEvent contextStartedEvent) throws InterruptedException {
//        System.out.println("EventListener -- started 接收事件参数：" + contextStartedEvent);
//    }
}
